var form;
var dataId = getTempData('updateDictDataId');

$(function () {
    initLayUI(function () {
        initData();
    });
});

function initLayUI(cb) {
    layui.use(['form'], function () {
        form = layui.form;

        form.render();

        form.on('submit(dictDataEditForm)', function (data) {
            let dictData = {
                "dictLabel": data.field.dictLabel,
                "dictValue": data.field.dictValue,
                "dictType": data.field.dictType,
                "orderNum": data.field.orderNum,
                "isDefault": data.field.isDefault
            };
            if (dataId !== undefined) {
                dictData.dataId = dataId;
                updateDictData(dictData);
            } else {
                addDictData(dictData);
            }
            return false;
        });

        $("#cancelBtn").click(function () {
            layerUtil.closeLayer(parent.window.iframeIndex);
        });

        cb();
    });
}

function addDictData(dict) {
    ajaxUtil.simplePost(dict, "/dictData/add", function () {
        layerUtil.closeLayer();
        reloadParentTable('dictDataTable');
    });
}

function updateDictData(dict) {
    ajaxUtil.simplePost(dict, "/dictData/update", function () {
        layerUtil.closeLayer();
        reloadParentTable('dictDataTable');
    });
}

function initData() {
    form.val('form', {
        "dictType": getTempData("updateDictDataType")
    });
    if (dataId !== undefined) {
        ajaxUtil.simpleGet("/dictData/queryDictDataById?dataId=" + dataId, function (data) {
            form.val('form', {
                "dictLabel": data.dictLabel,
                "dictValue": data.dictValue,
                "orderNum": data.orderNum,
                "isDefault": data.isDefault
            });
        });
    }
}